import os, sys
import time
try:
  from PIL import Image
  import numpy as np
except:
  os.system("pip3 install --user numpy Pillow")
  from PIL import Image
  import numpy as np

def main(a, a2):
  s = Image.open(a).convert('L')
  s2_w, s3_h = s.size
  s4 = s.resize((s2_w//2, s3_h//2))
  #s5 = "%s.%s.%s" %(os.path.basename(a).rsplit(".", 1)[0], time.time(), a.rsplit(".", 1)[1])
  s4.save(os.path.join(a2, os.path.basename(a)), quality=main.a3)

if __name__ == "__main__":
  main.a3 = 80
  s8 = False

  if len(sys.argv[1:]) == 0:
    print("将图片压缩为可上传的大小，如果需覆盖图片，请使用-p标识")
    sys.exit()
  for i in sys.argv[1:]:
    if i[:1]=='-':
      s6 = i.strip('-')
      if s6=='p' or s6=='path':
        s8 = True
        continue
      main.a3=int(s6)
      continue

  for i in sys.argv[1:]:
    if i[:1]!='-':
      i= i.strip('\'\"\\').rstrip("/")
      if os.path.isdir(i):
        if not s8:
          s7 = "%s.%s" %(os.path.basename(i), time.time())
          os.mkdir(s7)
          s7 = os.path.abspath(s7)
        else:
          s7 = i
        for i2 in os.listdir(i):
          main(os.path.join(i, i2), s7)
      else:
        main(i, '')
